package practise.alogrithm.hundredInterviewQuestions;

/**
 * get "1" amount in  a binary format of a number.
 * Note, it might be a negative value
 */
public class BinaryOneAmountInANumber {
    final static int MASK=1;
    public static int getAmountOfBinaryOne(int number){// in order to avoid the endless loop, we have to move the mask
        // as if the number is a negative number, the highest number always 1
        int count =0;
        int times =0;
        int newNumber = number;
        if(newNumber>(Integer.MAX_VALUE>>1)){
            count+=(newNumber&MASK);
            newNumber=newNumber/2;
        }
        while(Math.abs(newNumber)>=Math.abs(MASK<<times))
            count+=((newNumber&MASK<<times)>>(times++));
        return count;

    }

    public static void main(String[] args){
        System.out.print("Result:"+getAmountOfBinaryOne(4));
    }

}
